{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 处理并发请求\n",
    "### 背景\n",
    "fastapi接口处理请求时,一个接口处理方法阻塞,其他接口无法调用"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "INFO:     Started server process [18440]\n",
      "INFO:     Waiting for application startup.\n",
      "INFO:     Application startup complete.\n",
      "INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "info\n",
      "INFO:     127.0.0.1:43429 - \"GET /info HTTP/1.1\" 200 OK\n",
      "long\n",
      "<property object at 0x0000023507DE11C0> --> 0\n",
      "<property object at 0x0000023507DE11C0> --> 1\n",
      "<property object at 0x0000023507DE11C0> --> 2\n",
      "<property object at 0x0000023507DE11C0> --> 3\n",
      "<property object at 0x0000023507DE11C0> --> 4\n",
      "<property object at 0x0000023507DE11C0> --> 5\n",
      "<property object at 0x0000023507DE11C0> --> 6\n",
      "<property object at 0x0000023507DE11C0> --> 7\n",
      "<property object at 0x0000023507DE11C0> --> 8\n",
      "<property object at 0x0000023507DE11C0> --> 9\n",
      "<property object at 0x0000023507DE11C0> --> 10\n",
      "<property object at 0x0000023507DE11C0> --> 11\n",
      "<property object at 0x0000023507DE11C0> --> 12\n",
      "<property object at 0x0000023507DE11C0> --> 13\n",
      "<property object at 0x0000023507DE11C0> --> 14\n",
      "<property object at 0x0000023507DE11C0> --> 15\n",
      "<property object at 0x0000023507DE11C0> --> 16\n",
      "<property object at 0x0000023507DE11C0> --> 17\n",
      "<property object at 0x0000023507DE11C0> --> 18\n",
      "<property object at 0x0000023507DE11C0> --> 19\n",
      "INFO:     127.0.0.1:43429 - \"GET /long HTTP/1.1\" 200 OK\n",
      "info\n",
      "INFO:     127.0.0.1:43431 - \"GET /info HTTP/1.1\" 200 OK\n"
     ]
    }
   ],
   "source": [
    "from fastapi import FastAPI\n",
    "from fastapi.responses import PlainTextResponse, Response\n",
    "import uvicorn\n",
    "import time\n",
    "from threading import Thread\n",
    "from multiprocessing import Process\n",
    "\n",
    "app = FastAPI()\n",
    "\n",
    "@app.get('/info', response_class=PlainTextResponse)\n",
    "async def info():\n",
    "    print('info')\n",
    "    return 'info'\n",
    "\n",
    "@app.get('/hello')\n",
    "async def hello():\n",
    "    print('hello')\n",
    "    # t = Thread(target=stop, args=(20,))\n",
    "    # t.start()\n",
    "\n",
    "    p = Process(target=stop, args=(20,))\n",
    "    p.start()\n",
    "    return Response(content='hello')\n",
    "\n",
    "\n",
    "@app.get(\"/long\")\n",
    "async def long():\n",
    "    print('long')\n",
    "    stop(20)\n",
    "\n",
    "def stop(interval: int):\n",
    "    for i in range(interval):\n",
    "        time.sleep(1)\n",
    "        print(Process.name, \"-->\", str(i))\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    config = uvicorn.Config(app=app, host='127.0.0.1', port=8000, workers=2)\n",
    "    server = uvicorn.Server(config)\n",
    "    await server.serve()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": true
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
